<?php

namespace app\shop\controller\menu;

use app\shop\controller\Controller;
use app\shop\model\menu\Menu as MenuModel;
use app\shop\model\mould\Mould as MouldModel;

/**
 * 栏目
 */
class Menu extends Controller
{
    /**
     * 栏目列表
     */
    public function index()
    {
        $model = new MenuModel;
        $list = $model->getCacheTree();
        $mouldList = MouldModel::getALL();
        return $this->renderSuccess('', compact('list', 'mouldList'));
    }

    /**
     * 添加栏目
     */
    public function add()
    {
        $model = new MenuModel;
        // 新增记录
        if ($model->add($this->request->post())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑栏目
     */
    public function edit($menu_id)
    {
        // 详情
        $model = MenuModel::detail($menu_id);
        if ($this->request->isGet()) {
            return $this->renderSuccess('', compact('model'));
        }
        // 更新记录
        if ($model->edit($this->request->post())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 设置状态
     */
    public function set($menu_id)
    {
        // 详情
        $model = MenuModel::detail($menu_id);
        // 更新记录
        if ($model->setStatus($this->request->post())) {
            return $this->renderSuccess('设置成功');
        }
        return $this->renderError($model->getError() ?: '设置失败');
    }

    /**
     * 删除栏目
     */
    public function delete($menu_id)
    {
        $model = MenuModel::detail($menu_id);
        if ($model->remove($menu_id)) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?: '删除失败');
    }

}
